# -*- coding: utf-8 -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

__all__ = ('cmp', 'comparable')


def cmp(a, b):
	"""
	Compare two objects.

	Returns a negative number if C{a < b}, zero if they are equal, and a
	positive number if C{a > b}."""

	if a < b: return -1
	if a > b: return  1
	return 0


def comparable(klass):
	"""
	Class decorator that ensures support for the special C{__cmp__} method.

	C{__eq__}, C{__lt__}, etc. methods are added to the class, relying on
	C{__cmp__} to implement their comparisons."""

	def __eq__(self, other):
		c = self.__cmp__(other)
		if c is NotImplemented:
			return c
		return c == 0

	def __ne__(self, other):
		c = self.__cmp__(other)
		if c is NotImplemented:
			return c
		return c != 0

	def __lt__(self, other):
		c = self.__cmp__(other)
		if c is NotImplemented:
			return c
		return c < 0

	def __le__(self, other):
		c = self.__cmp__(other)
		if c is NotImplemented:
			return c
		return c <= 0

	def __gt__(self, other):
		c = self.__cmp__(other)
		if c is NotImplemented:
			return c
		return c > 0

	def __ge__(self, other):
		c = self.__cmp__(other)
		if c is NotImplemented:
			return c
		return c >= 0

	klass.__lt__ = __lt__
	klass.__gt__ = __gt__
	klass.__le__ = __le__
	klass.__ge__ = __ge__
	klass.__eq__ = __eq__
	klass.__ne__ = __ne__

	return klass
